﻿using BaseDll;
using System;
using System.Net;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.IO;

namespace OtherDevice
{
    [Description("DP螺丝机")]
    public class Screw_DP:Screw_Base
    {
        public Screw_DP(string strName) : base(strName)
        {
            controlFrom = new Form_DP螺丝机();
        }
        private bool GetData(out string Result)
        {
            string result = " ";
            
            //string url = $"http://10.10.25.100/cgi-bin/cgiread?site=13&dlfile=/mnt/mmc/finaldata/actual.csv";
            string url = $"http://{paramter.str_IP地址}/cgi-bin/cgiread?site=13&dlfile=/mnt/mmc/finaldata/actual.csv";
            System.GC.Collect();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Timeout = 500;
            req.KeepAlive = false;
            System.Net.ServicePointManager.DefaultConnectionLimit = 50;
            bool b_Live = req.KeepAlive;
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            try
            {
                //获取内容
                using (StreamReader reader = new StreamReader(stream))
                {
                    result = reader.ReadToEnd();
                }
            }
            catch (Exception)
            {
                stream.Close();
                Result = "";
                return false;
            }
            finally
            {
                stream.Close();
            }
            Result = result.TrimEnd((char[])"\n".ToCharArray());
            return true;
        }

        private string CutReciveDate(string str, int i)
        {
            if (str != "")
            {
                string[] dataArry = str.Split(',');
                return dataArry[i];
            }
            else
            {
                return "";
            }
        }

        public override bool Function_GetScrewTorque(out double d_Torque)
        {
            try
            {
                string reciveData = "";
                GetData(out reciveData);
                double Torque1 = Convert.ToDouble(CutReciveDate(reciveData, 6));
                double Torque2 = Convert.ToDouble(CutReciveDate(reciveData, 9));
                double Torque3 = Convert.ToDouble(CutReciveDate(reciveData, 12));
                if (Torque1 >= Torque2 && Torque1 >= Torque3)
                {
                    d_Torque = Convert.ToDouble(CutReciveDate(reciveData, 6));
                }
                else if (Torque2 >= Torque1 && Torque2 >= Torque3)
                {
                    d_Torque = Convert.ToDouble(CutReciveDate(reciveData, 9));
                }
                else if (Torque3 >= Torque1 && Torque3 >= Torque2)
                {
                    d_Torque = Convert.ToDouble(CutReciveDate(reciveData, 12));
                }
                else
                {
                    d_Torque = Convert.ToDouble(CutReciveDate(reciveData, 15));
                }
                return true;

            }
            catch (Exception)
            {
                d_Torque = 0;
                return false;
            }
        }

        public override bool Function_GetScrewAngle(out double d_Angle)
        {
            try
            {
                string reciveData = "";
                GetData(out reciveData);
                double data = Convert.ToDouble(CutReciveDate(reciveData, 7)) + Convert.ToDouble(CutReciveDate(reciveData, 10));
                data = data + Convert.ToDouble(CutReciveDate(reciveData, 13));
                if (data == 0)
                {
                    d_Angle = Convert.ToDouble(CutReciveDate(reciveData, 17));
                }
                else
                {
                    d_Angle = data;
                }
                return true;
            }
            catch (Exception)
            {
                d_Angle = 0;
                return false;
            }
        }
    }
}